import { Global } from './../../Global/index';
import { courses } from './../../shared/types/courses';
import { ApiCall } from "tsrpc";
import { ReqGetSelectedCourses, ResGetSelectedCourses } from "../../shared/protocols/Get/PtlGetSelectedCourses";

export default async function (call: ApiCall<ReqGetSelectedCourses, ResGetSelectedCourses>) {
    // TODO
    const student = call.currentUser

    const courses = await Global.collection('selectCourses').aggregate([
        {
            $match: {
                student: student._id
            }
        },
        {
            $lookup: {
                from: 'courses',
                localField: 'course',
                foreignField: '_id',
                as: 'course'
            }
        }
    ]).toArray()

    call.succ({
        courses:courses.map(item => item.course[0])
    })
}